<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="880px" @confirm="handleSubmit"
			@close="handleClose">
			<div class="mt-[-28px]">
				<el-card class="!border-none" shadow="never">
					<div class="">
						<el-table :data="vip.lists" style="width: 100%" height="260px" highlight-current-row
							@current-change="handleCurrentChange">
							<el-table-column label="等级名称" prop="name" show-overflow-tooltip />
							<el-table-column label="LOGO" min-width="60">
								<template #default="{ row }">
									<image-contain v-if="row.logo" :src="row.logo" :width="60" :height="45"
										:preview-src-list="[row.logo]" preview-teleported fit="contain" />
								</template>
							</el-table-column>
							<el-table-column label="有效金额" prop="money" show-overflow-tooltip />
							<el-table-column label="1代人数" prop="level1_num" show-overflow-tooltip />
							<el-table-column label="推荐人等级" prop="vip_name" show-overflow-tooltip />
							<el-table-column label="质押次数" prop="item_num" show-overflow-tooltip />
							<el-table-column label="质押加息率" prop="item_add_rate" show-overflow-tooltip />
							<el-table-column label="操作" fixed="right">
								<template #default="{ row }">
									点击选择
								</template>
							</el-table-column>
						</el-table>
					</div>
				</el-card>
			</div>
		</popup>
	</div>
</template>

<script lang="ts" setup name="goodsEdit">
	import Popup from '@/components/popup/index.vue'
	import type { PropType } from 'vue'
	import { apiUserMemberAll, apiSetUserMember } from '@/api/user_member'
	import feedback from '@/utils/feedback'
	import { isEmpty } from 'lodash-es'

	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const popupRef = shallowRef<InstanceType<typeof Popup>>()


	// 弹窗标题
	const popupTitle = computed(() => {
		return '会员等级'
	})
	
	const vip = reactive({
		lists: []
	})
	
	// 表单数据
	const formData = reactive({
		user_id: '',
		member_id: 0,
	})

	// 选中数据
	const currentRow = ref()

	// 表格选择后回调事件
	const handleCurrentChange = (val : string) => {
		currentRow.value = val
	}

	// 提交按钮
	const handleSubmit = async () => {
		if (!isEmpty(currentRow.value)) {
			formData.member_id = currentRow.value.id;
		}
		if (formData.member_id == 0) {
			return feedback.msgError('请选择会员等级')
		}
		await apiSetUserMember(formData)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = () => {
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}
	
	const getData = async () => {
		const res = await apiUserMemberAll()
		vip.lists = res;
	}

	const setFormData = async (data : Record<any, any>) => {
		formData.user_id = data.user_id;
	}

	defineExpose({
		open,
		setFormData,
	})
	getData()
</script>